package util.content;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import replaceWords.Complex;
import replaceWords.MarkDealXML;

public class ContentRex {

	List sourceSiteList = null;

	public String getTitle() {
		for (Iterator iterSourceSite = sourceSiteList.iterator(); iterSourceSite.hasNext();) {
			Element sourceSite = (Element) iterSourceSite.next();
			String titleRex = sourceSite.getChildText("titleRex");
			Pattern p = Pattern.compile(titleRex);
			Matcher m = p.matcher(title);
			if (m.find()) {
				title = m.group(1).trim();
			}
		}
		return title;
	}

	String contentString = "";

	public String getContentString() {

		String partialContent = null;
		ContentCapture cc = new ContentCapture();
		for (Iterator iterSourceSite = sourceSiteList.iterator(); iterSourceSite.hasNext();) {
			Element sourceSite = (Element) iterSourceSite.next();
			List spiderList = sourceSite.getChildren("spider");
			for (Iterator iterSpider = spiderList.iterator(); iterSpider.hasNext();) {
				Element spider = (Element) iterSpider.next();
				// 抓取文章内容
				List contentList = spider.getChildren("content");
				for (Iterator iterContent = contentList.iterator(); iterContent.hasNext();) {
					Element content = (Element) iterContent.next();
					// 抓取文章内容
					List contentRexList = content.getChildren("contentget");
					for (Iterator itercontentRexList = contentRexList.iterator(); itercontentRexList.hasNext();) {
						Element contentRexE = (Element) itercontentRexList.next();
						String contentRex = contentRexE.getText();
						// System.out.println(contentRex);
						// 调用ContentCapture对象
						cc.setContentRex(contentRex);
						partialContent = cc.getContent(bodyString);
						//System.out.println(partialContent);
					}
					List contentdelList = content.getChildren("contentdel");
					for (Iterator iterContentdel = contentdelList.iterator(); iterContentdel.hasNext();) {
						Element contentdel = (Element) iterContentdel.next();
						// 拿到需要删除的内容的正则
						String delRex = contentdel.getText();
						cc.setDelRex(delRex);
						//System.out.println(partialContent.replaceAll("<.*display:none.*>[\\w\\W]*?</.*>", "").replaceAll("<p.*>\\s", "<p>").replaceAll("<pre[\\w\\W]*?>\\s", "<pre>").replaceAll("<span[\\w\\W]*?>\\s", "<span>").trim());
						partialContent = cc.del(partialContent).replaceAll("<p.*>", "<p>").replaceAll("<pre[\\w\\W]*?>", "<pre>").replaceAll("<span[\\w\\W]*?>", "<span>").trim();						
						//System.out.println(partialContent);
					}
					// 这里拼了一个格式
					contentString = contentString + partialContent;
					//System.out.println(contentString);
					
				}
			}
		}
		//System.out.println(contentString);
		return contentString;
	}

	ContentRex() {
		SAXBuilder builder = new SAXBuilder(false);
		try {
			String xmlpath = "spiderParameter.xml";
			Document doc = builder.build(xmlpath);
			Element sourceSite = doc.getRootElement();
			sourceSiteList = sourceSite.getChildren("sourceSite");
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	String bodyString = null;

	public void setBodyString(String bodyString) {
		this.bodyString = bodyString;
	}

	String title = null;

	public void setTitle(String title) {
		this.title = title;
	}
}
